package com.atm.thirdplt.web.controller.busi;

import com.atm.thirdplt.common.core.domain.AjaxResult;
import com.atm.thirdplt.common.core.domain.his.HisOperationSchedule;
import com.atm.thirdplt.common.core.domain.his.HisScheduledOperationName;
import com.atm.thirdplt.common.utils.StringUtils;
import com.atm.thirdplt.component.HisDataHandler;
import com.atm.thirdplt.service.IScheduleSyncControllerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 医院His系统数据同步
 *
 * @author wenyf
 */
@Api(tags = "医院his系统数据同步")
@RestController
@RequestMapping("/his")
public class HisDataController {

    @Autowired
    private HisDataHandler hisDataHandler;

    @Autowired
    private IScheduleSyncControllerService scheduleSyncControllerService;

    @ApiOperation(value = "科室字典同步", notes = "科室字典同步")
    @GetMapping("/deptSync")
    public AjaxResult deptSync() {

        hisDataHandler.syncDeptDict();
        return AjaxResult.success();
    }

    @ApiOperation(value = "工作人员字典同步", notes = "工作人员字典同步")
    @GetMapping("/userSync")
    public AjaxResult userSync() {

        hisDataHandler.syncUsers();
        return AjaxResult.success();
    }

    @ApiOperation(value = "病人在院信息查询", notes = "病人在院信息查询")
    @GetMapping("/queryPatInHos")
    public AjaxResult queryPatInHos(@RequestParam(name = "patId", required = false) String patId,
                                    @RequestParam(name = "inpNo", required = false) String inpNo) {

        if (StringUtils.isEmpty(patId) && StringUtils.isEmpty(inpNo)) {
            return AjaxResult.error("patId and inpNo not all empty");
        }

        return AjaxResult.success(hisDataHandler.queryPatInHos(patId, inpNo));
    }

    @ApiOperation(value = "病人预约手术信息查询", notes = "病人预约手术信息查询")
    @GetMapping("/queryOperSche")
    public AjaxResult queryOperSche(@RequestParam(name = "patId") String patId,
                                    @RequestParam(name = "visitId", required = false) String visitId,
                                    @RequestParam(name = "startDate") String startDate,
                                    @RequestParam(name = "endDate") String endDate) {

        if (StringUtils.isEmpty(patId)) {
            return AjaxResult.error("patId is not null");
        }

        List<HisOperationSchedule> list = hisDataHandler.queryOperSche(patId, visitId, startDate, endDate);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success();
        }
        return AjaxResult.success(list);

    }

    @ApiOperation(value = "病人申请手术名称信息查询", notes = "病人申请手术名称信息查询")
    @GetMapping("/queryOperScheName")
    public AjaxResult queryOperScheName(@RequestParam(name = "patId") String patId,
                                        @RequestParam(name = "visitId") String visitId,
                                        @RequestParam(name = "scheduleId") String scheduleId) {

        if (StringUtils.isEmpty(patId) || StringUtils.isEmpty(visitId) || StringUtils.isEmpty(scheduleId)) {
            return AjaxResult.error("patId or visitId or scheduleId is all not null");
        }

        List<HisScheduledOperationName> list = hisDataHandler.queryOperScheName(patId, visitId, scheduleId);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success();
        }
        return AjaxResult.success(list);

    }

    @ApiOperation(value = "病人基本信息查询", notes = "病人基本信息查询")
    @GetMapping("/queryPaientBase")
    public AjaxResult queryPaientBase(@RequestParam(name = "patId") String patId) {
        return AjaxResult.success(hisDataHandler.queryPatMaster(patId));
    }

    @ApiOperation(value = "病人基本信息查询", notes = "根据门诊号查询基本信息")
    @GetMapping("/queryPatClinicMaster")
    public AjaxResult queryPatClinicMaster(@RequestParam(name = "clinicNumber") String clinicNumber) {
        return AjaxResult.success(hisDataHandler.queryPatClinicMaster(clinicNumber));
    }

    @ApiOperation(value = "手术预约信息同步", notes = "手术预约信息同步")
    @GetMapping("/scheduleSync")
    public AjaxResult scheduleSync(@RequestParam(name = "startDate") String startDate,
                                   @RequestParam(name = "endDate") String endDate) {

        Object status = hisDataHandler.syncSchedule(startDate, endDate);
        return AjaxResult.success(status);
    }

    @ApiOperation(value = "手术预约同步执行状态", notes = "手术预约同步执行状态")
    @GetMapping("/scheduleSyncFlag")
    public AjaxResult scheduleSyncFlag() {

        Object flag = hisDataHandler.getCurSyncStatus();
        return AjaxResult.success(flag);
    }

    @ApiOperation(value = "手术名称字典同步", notes = "手术名称字典同步")
    @GetMapping("/operDicSync")
    public AjaxResult operDicSync() {
        hisDataHandler.syncoperDic();
        return AjaxResult.success();
    }
}
